<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件预览 - {{ filename }}</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Microsoft YaHei', Arial, sans-serif;
        }
        body {
            background-color: #f5f5f5;
            color: #333;
            padding: 20px;
            max-width: 1200px;
            margin: 0 auto;
        }
        .container {
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
            margin-bottom: 20px;
        }
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 1px solid #eee;
        }
        .file-name {
            font-size: 18px;
            font-weight: bold;
            color: #333;
            word-break: break-all;
        }
        .back-link {
            display: inline-block;
            padding: 6px 12px;
            background-color: #2196F3;
            color: white;
            text-decoration: none;
            border-radius: 4px;
            font-size: 14px;
        }
        .back-link:hover {
            background-color: #0b7dda;
        }
        .preview-container {
            text-align: center;
            padding: 20px;
        }
        .preview-content {
            max-width: 100%;
            margin: 0 auto;
        }
        .unsupported-message, .error-message {
            text-align: center;
            padding: 50px 20px;
            color: #666;
        }
        .unsupported-message h2, .error-message h2 {
            margin-bottom: 15px;
            color: #f44336;
        }
        .unsupported-message p, .error-message p {
            margin-bottom: 20px;
        }
        
        /* 图片预览 */
        .img-preview {
            max-width: 100%;
            max-height: 80vh;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
        }
        
        /* 视频预览 */
        .video-preview {
            max-width: 100%;
            max-height: 80vh;
        }
        
        /* 音频预览 */
        .audio-preview {
            width: 100%;
            margin: 20px 0;
        }
        
        /* 文本预览 */
        .text-preview {
            width: 100%;
            max-height: 80vh;
            overflow: auto;
            white-space: pre-wrap;
            font-family: Consolas, Monaco, 'Andale Mono', monospace;
            font-size: 14px;
            line-height: 1.5;
            text-align: left;
            padding: 15px;
            border: 1px solid #eee;
            border-radius: 4px;
            background-color: #f9f9f9;
        }
        
        /* Markdown预览 */
        .markdown-container {
            display: flex;
            flex-direction: column;
            width: 100%;
            height: 80vh;
            border: 1px solid #eee;
            border-radius: 4px;
            overflow: hidden;
        }
        
        .markdown-tabs {
            display: flex;
            background-color: #f9f9f9;
            border-bottom: 1px solid #eee;
        }
        
        .markdown-tab {
            padding: 10px 15px;
            cursor: pointer;
            border-right: 1px solid #eee;
            font-size: 14px;
        }
        
        .markdown-tab.active {
            background-color: #fff;
            border-bottom: 2px solid #2196F3;
        }
        
        .markdown-content {
            flex: 1;
            overflow: auto;
            display: none;
            padding: 20px;
            text-align: left;
        }
        
        .markdown-content.active {
            display: block;
        }
        
        .markdown-raw {
            white-space: pre-wrap;
            font-family: Consolas, Monaco, 'Andale Mono', monospace;
            font-size: 14px;
            line-height: 1.5;
            padding: 15px;
            background-color: #f9f9f9;
        }
        
        .markdown-html {
            line-height: 1.6;
            font-size: 16px;
        }
        
        /* Markdown渲染样式 */
        .markdown-html h1, .markdown-html h2, .markdown-html h3, 
        .markdown-html h4, .markdown-html h5, .markdown-html h6 {
            margin-top: 1.5em;
            margin-bottom: 0.5em;
            font-weight: 600;
            line-height: 1.25;
        }
        
        .markdown-html h1 {
            font-size: 2em;
            border-bottom: 1px solid #eaecef;
            padding-bottom: 0.3em;
        }
        
        .markdown-html h2 {
            font-size: 1.5em;
            border-bottom: 1px solid #eaecef;
            padding-bottom: 0.3em;
        }
        
        .markdown-html h3 {
            font-size: 1.25em;
        }
        
        .markdown-html p, .markdown-html ul, .markdown-html ol {
            margin-bottom: 16px;
        }
        
        .markdown-html a {
            color: #0366d6;
            text-decoration: none;
        }
        
        .markdown-html a:hover {
            text-decoration: underline;
        }
        
        .markdown-html blockquote {
            padding: 0 1em;
            color: #6a737d;
            border-left: 0.25em solid #dfe2e5;
            margin-bottom: 16px;
        }
        
        .markdown-html pre {
            padding: 16px;
            overflow: auto;
            line-height: 1.45;
            background-color: #f6f8fa;
            border-radius: 3px;
            margin-bottom: 16px;
        }
        
        .markdown-html code {
            padding: 0.2em 0.4em;
            margin: 0;
            font-size: 85%;
            background-color: rgba(27,31,35,0.05);
            border-radius: 3px;
        }
        
        .markdown-html pre code {
            padding: 0;
            background-color: transparent;
        }
        
        .markdown-html img {
            max-width: 100%;
            box-sizing: content-box;
        }
        
        .markdown-html table {
            border-collapse: collapse;
            width: 100%;
            margin-bottom: 16px;
        }
        
        .markdown-html table th, .markdown-html table td {
            padding: 6px 13px;
            border: 1px solid #dfe2e5;
        }
        
        .markdown-html table tr {
            background-color: #fff;
            border-top: 1px solid #c6cbd1;
        }
        
        .markdown-html table tr:nth-child(2n) {
            background-color: #f6f8fa;
        }
        
        /* PDF预览 */
        .pdf-preview {
            width: 100%;
            height: 80vh;
            border: none;
        }
        
        /* Office预览 */
        .office-preview {
            width: 100%;
            height: 80vh;
            border: none;
        }
        
        .preview-tabs {
            display: flex;
            justify-content: center;
            margin-bottom: 15px;
        }
        
        .tab-button {
            padding: 8px 12px;
            margin: 0 5px;
            background-color: #f1f1f1;
            border: 1px solid #ddd;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }
        
        .tab-button.active {
            background-color: #4CAF50;
            color: white;
            border-color: #4CAF50;
        }
        
        .preview-tab-content {
            display: none;
        }
        
        .preview-tab-content.active {
            display: block;
        }
        
        .loading-message {
            text-align: center;
            padding: 50px 20px;
            color: #666;
        }
        
        .loading-spinner {
            border: 5px solid #f3f3f3;
            border-top: 5px solid #3498db;
            border-radius: 50%;
            width: 50px;
            height: 50px;
            animation: spin 2s linear infinite;
            margin: 20px auto;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .office-icon {
            width: 48px;
            height: 48px;
            margin-bottom: 15px;
        }
        
        .office-type {
            margin-bottom: 10px;
            font-weight: bold;
            color: #555;
        }
        
        @media (max-width: 768px) {
            body {
                padding: 10px;
            }
            .container {
                padding: 15px;
            }
            .preview-container {
                padding: 10px;
            }
            .header {
                flex-direction: column;
                align-items: flex-start;
            }
            .back-link {
                margin-top: 10px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <div class="file-name">{{ filename }}</div>
            <a href="/" class="back-link">返回文件列表</a>
        </div>
        
        <div class="preview-container">
            {% if preview_type == "image" %}
                <img src="/view/{{ filename }}" alt="{{ filename }}" class="img-preview preview-content">
            
            {% elif preview_type == "video" %}
                <video controls class="video-preview preview-content">
                    <source src="/view/{{ filename }}" type="video/mp4">
                    您的浏览器不支持视频播放
                </video>
            
            {% elif preview_type == "audio" %}
                <audio controls class="audio-preview preview-content">
                    <source src="/view/{{ filename }}">
                    您的浏览器不支持音频播放
                </audio>
            
            {% elif preview_type == "text" %}
                <pre class="text-preview preview-content">{{ content }}</pre>
            
            {% elif preview_type == "markdown" %}
                <div class="markdown-container">
                    <div class="markdown-tabs">
                        <div class="markdown-tab active" onclick="switchMarkdownTab('rendered')">已渲染</div>
                        <div class="markdown-tab" onclick="switchMarkdownTab('source')">源代码</div>
                    </div>
                    <div id="rendered-tab" class="markdown-content markdown-html active">
                        {{ html_content|safe }}
                    </div>
                    <div id="source-tab" class="markdown-content markdown-raw">
                        {{ raw_content }}
                    </div>
                </div>
                
                <script>
                    function switchMarkdownTab(tabId) {
                        // 隐藏所有内容
                        document.querySelectorAll('.markdown-content').forEach(function(tab) {
                            tab.classList.remove('active');
                        });
                        
                        // 取消激活所有标签
                        document.querySelectorAll('.markdown-tab').forEach(function(tab) {
                            tab.classList.remove('active');
                        });
                        
                        // 显示选中的内容和激活标签
                        document.getElementById(tabId + '-tab').classList.add('active');
                        if (tabId === 'rendered') {
                            document.querySelectorAll('.markdown-tab')[0].classList.add('active');
                        } else {
                            document.querySelectorAll('.markdown-tab')[1].classList.add('active');
                        }
                    }
                </script>
            
            {% elif preview_type == "pdf" %}
                <iframe src="/view/{{ filename }}" class="pdf-preview preview-content"></iframe>
            
            {% elif preview_type == "office" %}
                <div class="office-type">
                    {% if office_type == 'excel' %}
                        <img src="https://img.icons8.com/color/48/000000/microsoft-excel-2019--v1.png" alt="Excel" class="office-icon">
                        <div>Microsoft Excel文档</div>
                    {% elif office_type == 'word' %}
                        <img src="https://img.icons8.com/color/48/000000/microsoft-word-2019--v1.png" alt="Word" class="office-icon">
                        <div>Microsoft Word文档</div>
                    {% elif office_type == 'powerpoint' %}
                        <img src="https://img.icons8.com/color/48/000000/microsoft-powerpoint-2019--v1.png" alt="PowerPoint" class="office-icon">
                        <div>Microsoft PowerPoint文档</div>
                    {% else %}
                        <img src="https://img.icons8.com/color/48/000000/microsoft-office-2019.png" alt="Office" class="office-icon">
                        <div>Office文档</div>
                    {% endif %}
                </div>

                <div class="preview-tabs">
                    <button class="tab-button active" onclick="switchTab('ms-office')">Microsoft Office预览</button>
                    <button class="tab-button" onclick="switchTab('google-docs')">Google Docs预览</button>
                    <a href="/download/{{ filename }}" class="tab-button">下载文件</a>
                </div>
                
                <div id="ms-office-tab" class="preview-tab-content active">
                    <div class="loading-message" id="ms-office-loading">
                        <div class="loading-spinner"></div>
                        <p>正在加载Microsoft Office预览器，这可能需要几秒钟...</p>
                    </div>
                    <iframe id="ms-office-frame" src="{{ preview_urls.ms_office }}" class="office-preview preview-content" onload="hideLoading('ms-office')"></iframe>
                </div>
                
                <div id="google-docs-tab" class="preview-tab-content">
                    <div class="loading-message" id="google-docs-loading">
                        <div class="loading-spinner"></div>
                        <p>正在加载Google Docs预览器，这可能需要几秒钟...</p>
                    </div>
                    <iframe id="google-docs-frame" src="{{ preview_urls.google_docs }}" class="office-preview preview-content" onload="hideLoading('google-docs')"></iframe>
                </div>
                
                <script>
                    function switchTab(tabId) {
                        // 隐藏所有标签内容
                        document.querySelectorAll('.preview-tab-content').forEach(function(tab) {
                            tab.classList.remove('active');
                        });
                        
                        // 取消所有标签按钮激活状态
                        document.querySelectorAll('.tab-button').forEach(function(button) {
                            button.classList.remove('active');
                        });
                        
                        // 显示选中的标签内容
                        document.getElementById(tabId + '-tab').classList.add('active');
                        
                        // 激活对应的标签按钮
                        document.querySelectorAll('.tab-button').forEach(function(button) {
                            if (button.textContent.toLowerCase().includes(tabId)) {
                                button.classList.add('active');
                            }
                        });
                    }
                    
                    function hideLoading(frameId) {
                        setTimeout(function() {
                            document.getElementById(frameId + '-loading').style.display = 'none';
                        }, 1000);
                    }
                </script>
            
            {% elif preview_type == "unsupported" %}
                <div class="unsupported-message">
                    <h2>无法预览此文件类型</h2>
                    <p>该文件类型不支持在线预览，请下载后查看。</p>
                    <a href="/download/{{ filename }}" class="back-link">下载文件</a>
                </div>
            
            {% elif preview_type == "error" %}
                <div class="error-message">
                    <h2>预览出错</h2>
                    <p>在尝试预览文件时发生错误，请下载后查看。</p>
                    <a href="/download/{{ filename }}" class="back-link">下载文件</a>
                </div>
            {% endif %}
        </div>
    </div>
</body>
</html> 